home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / tpwprog6.arj / COMBO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-07-02  |  5.2 KB  |  206 lines

  1. { combo.pas -- Interactive combo box dialogs }
  2.  
  3. program Combo;
  4.  
  5. {$R combo.res}
  6.  
  7. uses WinTypes, WinProcs, WObjects, Strings;
  8.  
  9. const
  10.  
  11.   id_Menu     = 100;    { Menu resource ID }
  12.   cm_Dialog   = 101;    { Dialog-command ID }
  13.   id_Dialog   = 200;    { Dialog resource ID }
  14.  
  15.   id_Simple   = 101;    { Simple combo ID }
  16.   id_DropDown = 102;    { Drop-down combo ID }
  17.   id_DDList   = 103;    { Drop-down-list combo ID }
  18.  
  19.   numCountries = 8;
  20.   Countries: array[0 .. numCountries - 1] of PChar = (
  21.     'Great Britain',
  22.     'France',
  23.     'Germany',
  24.     'Sweden',
  25.     'United States',
  26.     'Mexico',
  27.     'Canada',
  28.     'Australia'
  29.   );
  30.  
  31.   numStates = 13;
  32.   States: Array[0 .. numStates - 1] of PChar = (
  33.     'Arkansas',
  34.     'Ohio',
  35.     'Virginia',
  36.     'Maryland',
  37.     'Nevada',
  38.     'Montana',
  39.     'Pennsylvania',
  40.     'California',
  41.     'Florida',
  42.     'Georgia',
  43.     'Arizona',
  44.     'Texas',
  45.     'New Hampshire'
  46.   );
  47.  
  48.   numCities = 8;
  49.   Cities: Array[0 .. numCities - 1] of PChar = (
  50.     'Philadelphia',
  51.     'Peterborough',
  52.     'Baltimore',
  53.     'Phoenix',
  54.     'San Antonio',
  55.     'Key West',
  56.     'San Francisco',
  57.     'Atlanta'
  58.   );
  59.  
  60. type
  61.  
  62.   PComboDialog = ^ComboDialog;
  63.   ComboDialog = object(TDialog)
  64.     procedure SetupWindow; virtual;
  65.     procedure Ok(var Msg: TMessage);
  66.       virtual id_First + id_Ok;
  67.   end;
  68.  
  69.   ComboApplication = object(TApplication)
  70.     procedure InitMainWindow; virtual;
  71.   end;
  72.  
  73.   PComboWindow = ^ComboWindow;
  74.   ComboWindow = object(TWindow)
  75.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  76.     procedure CMDialog(var Msg: TMessage);
  77.       virtual cm_First + cm_Dialog;
  78.   end;
  79.  
  80.  
  81. { Common routines }
  82.  
  83. {- Add string addressed by P to combo box identified by CtrlID }
  84. procedure SetCombo(HDlg: HWnd; CtrlID: Word; P: PChar);
  85. begin
  86.   SendDlgItemMessage(HDlg, CtrlID, cb_AddString, 0, LongInt(P))
  87. end;
  88.  
  89. {- Retrieve selected string (if any), allocating a buffer on the
  90. global heap, and returning in P the address of that buffer. If no
  91. string is selected or an error occurs, P is set to nil. If P is
  92. not nil, then Len equals the length in bytes of the allocated
  93. buffer, including room for a null terminator. After using GetCombo,
  94. if P is not nil, call FreeMem(P, Len) to dispose of the buffer. }
  95.  
  96. procedure GetCombo(HDlg: HWnd; CtrlID: Word; var P: PChar;
  97.   var Len: Word);
  98. var
  99.   Index: Integer;
  100.   HControl: HWnd;
  101. begin
  102.   P := nil;  { Default value in case of any errors }
  103.   HControl := GetDlgItem(HDlg, CtrlID);
  104.   Index := SendMessage(HControl, cb_GetCurSel, 0, 0);
  105.   if Index <> cb_Err then
  106.   begin
  107.     Len := SendMessage(HControl, cb_GetLbTextLen, Index, 0);
  108.     if Len > 0 then
  109.     begin
  110.       GetMem(P, Len + 1);  { Add 1 byte for null terminator }
  111.       if P <> nil then
  112.         SendMessage(HControl, cb_GetLbText, Index, LongInt(P))
  113.     end
  114.   end
  115. end;
  116.  
  117.  
  118. { ComboDialog }
  119.  
  120. {- Prepare the dialog window's contents }
  121. procedure ComboDialog.SetupWindow;
  122. var
  123.   I: Integer;
  124. begin
  125.   TDialog.SetupWindow;
  126.   for I := 0 to numCountries - 1 do
  127.     SetCombo(HWindow, id_Simple, Countries[I]);
  128.   for I := 0 to numStates - 1 do
  129.     SetCombo(HWindow, id_DropDown, States[I]);
  130.   for I := 0 to numCities - 1 do
  131.     SetCombo(HWindow, id_DDList, Cities[I])
  132. end;
  133.  
  134. {- Respond to Ok button }
  135. procedure ComboDialog.Ok(var Msg: TMessage);
  136. var
  137.   S1, S2, S3: PChar;          { Pointers to combo-box selections }
  138.   L1, L2, L3: Word;           { Length in bytes of S1..S3 arrays }
  139.   S4: Array[0 .. 64] of Char; { Holds concatenated selections }
  140. begin
  141. {- Initialize S4 to a null string }
  142.   S4[0] := Chr(0);
  143.  
  144. {- Get combo listbox selections, if any }
  145.   GetCombo(HWindow, id_Simple, S1, L1);
  146.   GetCombo(HWindow, id_DropDown, S2, L2);
  147.   GetCombo(HWindow, id_DDList, S3, L3);
  148.  
  149. {- Build result string in S4, ignoring nil pointers }
  150.   if S1 <> nil then StrCat(S4, S1);
  151.   StrCat(S4, ', ');
  152.   if S2 <> nil then StrCat(S4, S2);
  153.   StrCat(S4, ', ');
  154.   if S3 <> nil then StrCat(S4, S3);
  155.  
  156. {- Dispose the temporary strings if not nil }
  157.   if S1 <> nil then FreeMem(S1, L1);
  158.   if S2 <> nil then FreeMem(S2, L2);
  159.   if S3 <> nil then FreeMem(S3, L3);
  160.  
  161. {- Display selections, then continue dialog processing }
  162.   MessageBox(HWindow, S4, 'Your selections are...', mb_Ok);
  163.   TDialog.Ok(Msg)
  164. end;
  165.  
  166. { ComboApplication }
  167.  
  168. {- Initialize ComboApplication object's window }
  169. procedure ComboApplication.InitMainWindow;
  170. begin
  171.   MainWindow := New(PComboWindow, Init(nil, 'Combo'))
  172. end;
  173.  
  174.  
  175. { ComboWindow }
  176.  
  177. {- Construct ComboWindow object }
  178. constructor ComboWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  179. begin
  180.   TWindow.Init(AParent, ATitle);
  181.   Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
  182. end;
  183.  
  184. {- Execute Menu:Dialog command }
  185. procedure ComboWindow.CMDialog(var Msg: TMessage);
  186. begin
  187.   Application^.ExecDialog(New(PComboDialog,
  188.     Init(@Self, PChar(id_Dialog))))
  189. end;
  190.  
  191. var
  192.  
  193.   ComboApp: ComboApplication;
  194.  
  195. begin
  196.   ComboApp.Init('ComboApp');
  197.   ComboApp.Run;
  198.   ComboApp.Done
  199. end.
  200.  
  201.  
  202. {--------------------------------------------------------------
  203.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  204.   Revision 1.00    Date: 3/28/1991
  205. ---------------------------------------------------------------}
  206.